﻿YUI.add('editar-parcial-view', function (Y) {
    'use strict';
    Y.EditarParcial = Y.Base.create('EditarParcial', Y.View, [], {
        templateIsCharged: false,
        loadTemplate: function () {
            if (this.template == '') {
                var that = this,
                validator = null,
                request = Y.io('/ExcellentProject/scripts/views/calificaciones/parciales/templates/editarParcial.html', {
                    on: {
                        complete: function (id, response) {
                            that.templateIsCharged = true;
                            that.template = response.responseText;
                            that.render();
                        }
                    },
                    failure: function (id, response) {
                        alert(response)
                    }
                })
            }
        },
        events: {
            '.guardar': {
                click: 'guardarParcial'
            },
            '.cancelar': {
                click: 'cancelarParcial'
            }
        },

        render: function () {
            this.loadTemplate();
            if (this.templateIsCharged) {
                var contentTemplate = Y.Handlebars.compile(this.template);
                this.get('container').setHTML(contentTemplate(this.get('model').getAttrs()));

                if (this.get('model').get('Id') != 0) {
                    var statusListSelected = Y.one('#estadoParcial');
                    statusListSelected.set('value', this.get('model').get('Estado'));
                }
                Y.one('#navigatePeriodo').set('href', '/periodos/' + this.get('periodo'))
            }
            $('#inputNombre').alphanum();
            $('#inputDescripcion').alphanum();
        },

        guardarParcial: function (ev) {
            var model = this.get('model');
            var idPeriodo = this.get('periodo');
            model.setAttrs({
                Nombre: Y.one('#nombre').get('value'),
                Descripcion: Y.one('#descripcion').get('value'),
                FechaInicio: Y.one('#fechaInicio').get('value'),
                FechaFin: Y.one('#fechaFin').get('value'),
                Estado: Y.one('#estadoParcial').get('value'),
                IdPeriodo: idPeriodo
            });
            var options = null;
            if (model.get('Id') != 0)
                options = { action: 'update', idPar: model.get('IdParcial') };
            else
                options = { urlName: 'saveOption' };
            var that = this, a = model.save(options, function (err, response) {
                if (err != null) {
                    bootbox.dialog({
                        message: Y.JSON.parse(response.response).ExceptionMessage,
                        title: "Ocurrieron problemas al grabar el parcial",
                        show: true,
                        backdrop: true,
                        closeButton: true,
                        animate: true,

                        className: "my-modal",
                        buttons: {
                            error: {
                                label: "Ok",
                                className: "btn btn-danger",
                                callback: function () { }
                            }
                        }
                    });

                }
                else {
                    bootbox.dialog({
                        message: "<h5>Parcial creado</h5>",
                        title: "Confirmación",
                        show: true,
                        backdrop: true,
                        closeButton: true,
                        animate: true,

                        className: "my-modal",
                        buttons: {
                            success: {
                                label: "Ok",
                                className: "btn btn-success",
                                callback: function () { }
                            }
                        }
                    });
                    Y.one('#navigatePeriodo').simulate("click");

                }
            });
        },

        cancelarParcial: function (ev) {
            Y.one('#navigatePeriodo').simulate("click");
        }
    })

}, '0.0.0.1', { requires: ['view', 'handlebars', 'node'] })